home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 May / PCPlus May 1998=disk A.iso / full / CBUILDER / SAMS / SAMPLES / CHAP09 / JJMAIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-12  |  3.8 KB  |  144 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl\vcl.h>
  3. //
  4. // have to add this for the PlaySound() function
  5. //
  6. #include <vcl\mmsystem.hpp>
  7. #pragma hdrstop
  8. long DLLInstance;
  9. HINSTANCE _import GetInstance();
  10.  
  11. #include "JJMain.h"
  12. //---------------------------------------------------------------------------
  13. #pragma resource "*.dfm"
  14. //
  15. // here's where we add the resource file to the project
  16. //
  17. #pragma resource "JJRes.res"
  18. //
  19. // defines for the string resources
  20. //
  21. #define IDS_UP    101
  22. #define IDS_DOWN  102
  23.  
  24. TMainForm *MainForm;
  25. //---------------------------------------------------------------------------
  26. __fastcall TMainForm::TMainForm(TComponent* Owner)
  27.   : TForm(Owner),
  28.   done(false)
  29. {
  30. }
  31. //---------------------------------------------------------------------------
  32. void __fastcall TMainForm::FormCreate(TObject *Sender)
  33. {
  34.   //
  35.   // load and display the first bitmap
  36.   //
  37.   Image->Picture->Bitmap->
  38.     LoadFromResourceName((int)HInstance, "ID_BITMAP1");
  39. }
  40. //---------------------------------------------------------------------
  41. void __fastcall TMainForm::StartClick(TObject *Sender)
  42. {
  43.   //
  44.   // When the Start button is clicked the animation
  45.   // loop starts. The bitmap resources are named
  46.   // ID_BITMAP1 through ID_BITMAP5 so we'll start with
  47.   // a string called "ID_BITMAP" and append the last
  48.   // digit when needed.
  49.   //
  50.   String s = "ID_BITMAP";
  51.   //
  52.   // a buffer for the string resources
  53.   //
  54.   char buff[10];
  55.   //
  56.   // a flag to let us know when we're done
  57.   //
  58.   done = false;
  59.   //
  60.   // start the loop and keep looping until the 'Stop'
  61.   // button is pressed
  62.   //
  63.   while (!done) {
  64.     //
  65.     // loop through the five bitmaps starting with
  66.     // 1 and ending with 5
  67.     //
  68.     for (int i=1;i<6;i++) {
  69.       //
  70.       // append the value of 'i' to the end of the string
  71.       // to build a string containing the resource name
  72.       //
  73.       String resName = s + String(i);
  74.       //
  75.       // call a class member function to display the bitmap
  76.       //
  77.       DrawImage(resName);
  78.     }
  79.     //
  80.     // load the "Up" string resource using the WinAPI
  81.     // function LoadString(), display the string,
  82.     // and tell Windows to repaint the Label
  83.     //
  84.     LoadString(HInstance, IDS_UP, buff, sizeof(buff));
  85.     Label->Caption = buff;
  86.     Label->Refresh();
  87.     //
  88.     // play the 'up' sound using the WinAPI function
  89.     // PlaySound(), play it asynchronously
  90.     //
  91.     PlaySound("ID_WAVEUP",
  92.       HInstance, SND_ASYNC | SND_RESOURCE);
  93.     //
  94.     // pause for a moment at the top of the jump
  95.     //
  96.     Sleep(200);
  97.     //
  98.     // repeat all of the above except in reverse
  99.     //
  100.     for (int i=5;i>0;i--) {
  101.       String resName = s + String(i);
  102.       DrawImage(resName);
  103.     }
  104.     PlaySound("ID_WAVEDOWN",
  105.       HInstance, SND_ASYNC | SND_RESOURCE);
  106.     LoadString(HInstance, IDS_DOWN, buff, sizeof(buff));
  107.     Label->Caption = buff;
  108.     Label->Refresh();
  109.     Sleep(200);
  110.   }
  111. }
  112. //---------------------------------------------------------------------
  113. void __fastcall TMainForm::StopClick(TObject *Sender)
  114. {
  115.   //
  116.   // Stop button pressed, so tell the loop to stop executing
  117.   //
  118.   done = true;
  119. }
  120. //---------------------------------------------------------------------
  121. //
  122. // a class member function to display the bitmap
  123. //
  124. void
  125. TMainForm::DrawImage(String& name)
  126. {
  127.   //
  128.   // load the bitmap from a resource
  129.   // using the name passed to us
  130.   //
  131.   Image->Picture->Bitmap->
  132.     LoadFromResourceName((int)HInstance, name);
  133.   //
  134.   // must pump the message loop so that Windows gets
  135.   // a chance to display the bitmap
  136.   //
  137.   Application->ProcessMessages();
  138.   //
  139.   // take a short nap so the animation doesn't go too fast
  140.   //
  141.   Sleep(20);
  142. }
  143. //---------------------------------------------------------------------
  144.